# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Martell Malone <martellmalone@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

pkgname=pacman
pkgver=5.1.1
pkgrel=2
pkgdesc="A library-based package manager with dependency support (MSYS2 port)"
arch=('i686' 'x86_64')
url="https://www.archlinux.org/pacman/"
license=('GPL')
groups=('base' 'base-devel')
depends=('bash>=4.2.045'
         #'dash'
         #'busybox'
         'gettext'
         'gnupg'
         'msys2-runtime'
         #'libarchive'
         'curl'
         #'libgpgme'
         'pacman-mirrors'
         'msys2-keyring'
         'which'
         'bzip2'
         'xz')
optdepends=('diffutils' 'vim')
checkdepends=('python2')
makedepends=('asciidoc'
             'autoconf'
             'automake-wrapper'
             'doxygen'
             'libtool'
             'gcc'
             'git'
             'gettext-devel'
             'heimdal-devel'
             'libarchive-devel'
             'libcurl-devel'
             'libgpgme-devel'
             'make'
             'patch'
             'pkg-config'
             'libsqlite-devel'
             'libunistring-devel')
provides=('pacman-contrib')
conflicts=('pacman-contrib')
replaces=('pacman-contrib')
# options=('!strip' 'debug')
backup=("etc/pacman.conf"
        "etc/makepkg.conf"
        "etc/makepkg_mingw32.conf"
        "etc/makepkg_mingw64.conf")
source=(https://sources.archlinux.org/other/pacman/${pkgname}-${pkgver}.tar.gz{,.sig}
        https://git.archlinux.org/pacman-contrib.git/snapshot/pacman-contrib-1.0.0.tar.xz
        "pacman.conf"
        "makepkg.conf"
        "makepkg_mingw32.conf"
        "makepkg_mingw64.conf"
        "makepkg-mingw"
        "0000-pacman-msysize.patch"
        "0001-more-debugging-info.patch"
        "0002-Add-util-msys2.-c-h-and-rebase-db-msys2.-c.patch"
        "0003-use-busybox-for-msys2-post-installs.patch"
        "0004-Link-pacman-with-static-libraries.patch"
        "0005-Hack-gettext-libalpm-pkg-config-static-link.patch"
        "0006-makepkg-avoid-creating-.tar-files-with-extended-attr.patch"
        "0007-exe-interp-ignore-file-conflict.patch"
        "0008-answer-yes-by-default.patch"
        "0009-msys-use-pipe-instead-socket.patch"
        "0050-fix-make-target-dependencies.patch"
        "0100-contrib.patch")
sha256sums=('be04b9162d62d2567e21402dcbabb5bedfdb03909fa5ec6e8568e02ab325bd8d'
            'SKIP'
            'd47240396476eaf126b2f2a3f6ac77d5b397f5cc80b1c3700ba4fa355c4786c8'
            '6024bbf50cc92236b7b437430cb9e4180da91925cdc19a5a7910fe172931cfb6'
            '3d2f8a8c363a76ebd417e883cf28dc093e611b1360fe684d40e29540cbcb7ef2'
            '364552567f4d436e12f5b19634dd06ed0a9ae92a72d3e3f08ccbfaa8b69f48a5'
            'c0e4f55fa0c4b7b9f772f59f0f3d5e61cabc81cad770ed84171e833f73b7d034'
            '40109ac4f4e200ae9f76864231527f5f1049c27800e78d04656fcf5844ed789d'
            '8595c574e8b475591107e553832e4fbc93373769d9d193007d4512454e47a9ea'
            '24ea2c8dca37847e04894ebfd05d1cf5df49dc0c8089f5581c99caa19b77a7ef'
            '870b197b7d6379a9c1ebb5c449c902b21d75ec21e966a2e54af82501465180f7'
            '23132552a388b238acf8bf650b5c2aa08cf3de63c647e84ad551807c4edfeb1e'
            '65906109a7b71a9b836566ea888532b41f4ba6ea5996f1f3303c5789e03960cb'
            '0ffa9fbbea35054996075d5ca952a83e9b6357369dfdc2dc091ed789838205fc'
            '790ade0712c938ca03760f4f1e8ce2880016a4602a0c21975692940e4a46a28e'
            '52343bbf99da6408950e34dd84319e34724040f866c497c25e36b4ee54030e82'
            'e4f6e17af19e17e745a9f1c6b8402f5896229062c82167cb61f8e7d29eda716c'
            '9e8fe5ee78192b0407e80ad2e52cb27569c35974b6c26e465e3d55e19c03d108'
            '5bd66342aff56343aa5e07dbf997d18cc3dcae691cc7d8f83e94fee12b5b61b8'
            '3bd0cd48d608b31d7df564c50e8d31df58dd38cfaaea9ec0fcceb42936486549')

prepare() {
  cd ${srcdir}/${pkgname}-${pkgver}
  patch -p1 -i ${srcdir}/0000-pacman-msysize.patch
  patch -p1 -i ${srcdir}/0001-more-debugging-info.patch
  # patch -p1 -i "${srcdir}"/0002-Add-util-msys2.-c-h-and-rebase-db-msys2.-c.patch
  # patch -p1 -i "${srcdir}"/0003-use-busybox-for-msys2-post-installs.patch
  patch -p1 -i ${srcdir}/0004-Link-pacman-with-static-libraries.patch
  patch -p1 -i ${srcdir}/0005-Hack-gettext-libalpm-pkg-config-static-link.patch
  patch -p1 -i ${srcdir}/0006-makepkg-avoid-creating-.tar-files-with-extended-attr.patch
  patch -p1 -i ${srcdir}/0007-exe-interp-ignore-file-conflict.patch
  patch -p1 -i ${srcdir}/0008-answer-yes-by-default.patch
  patch -p1 -i ${srcdir}/0009-msys-use-pipe-instead-socket.patch
  patch -p1 -i ${srcdir}/0050-fix-make-target-dependencies.patch

  test "../common/util-common.h" != "$(cat src/util/util-common.h)" ||
  cp src/common/util-common.[ch] src/util/

  autoreconf -fi

  cd ${srcdir}/${pkgname}-contrib-1.0.0
  patch -p1 -i ${srcdir}/0100-contrib.patch
  ./autogen.sh
}

build() {
  cd ${srcdir}/${pkgname}-${pkgver}

  PKG_CONFIG="/usr/bin/pkg-config --static" \
  ./configure \
    --prefix=/usr \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --enable-static \
    --disable-shared \
    --enable-doc \
    --enable-doxygen \
    --disable-git-version \
    --with-scriptlet-shell=/usr/bin/bash \
    --with-pkg-ext=.pkg.tar.xz

  make -j3

  cd ${srcdir}/${pkgname}-contrib-1.0.0
  ./configure \
    --prefix=/usr \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --disable-git-version

  make
  
}

check() {
  make -C "${pkgname}-${pkgver}" check
}

package() {
  cd ${srcdir}/${pkgname}-${pkgver}
  make -j1 DESTDIR=${pkgdir} install

  cd ${srcdir}/${pkgname}-contrib-1.0.0
  make -j1 DESTDIR=${pkgdir} install

  # install Arch specific stuff
  install -dm755 ${pkgdir}/etc
  install -m644 ${srcdir}/pacman.conf ${pkgdir}/etc/pacman.conf

  case "${CARCH}" in
  i686)
    mycarch="i686"
    mychost="i686-pc-msys"
    myflags="-march=i686"
  ;;
  x86_64)
    mycarch="x86_64"
    mychost="x86_64-pc-msys"
    myflags="-march=x86-64"
  ;;
  esac

  install -m644 ${srcdir}/makepkg.conf ${pkgdir}/etc/
  install -m644 ${srcdir}/makepkg_mingw32.conf ${pkgdir}/etc/
  install -m644 ${srcdir}/makepkg_mingw64.conf ${pkgdir}/etc/
  install -m755 ${srcdir}/makepkg-mingw ${pkgdir}/usr/bin/

  # set things correctly in the default conf file
  sed -i ${pkgdir}/etc/makepkg.conf \
    -e "s|@CARCH[@]|${mycarch}|g" \
    -e "s|@CHOST[@]|${mychost}|g" \
    -e "s|@CARCHFLAGS[@]|${myflags}|g"

  # put bash_completion in the right location
  install -dm755 ${pkgdir}/usr/share/bash-completion/completions
  mv ${pkgdir}/etc/bash_completion.d/pacman \
    ${pkgdir}/usr/share/bash-completion/completions
  rmdir ${pkgdir}/etc/bash_completion.d

  for f in makepkg pacman-key; do
    ln -s pacman "${pkgdir}/usr/share/bash-completion/completions/$f"
  done
}
